import pygame
from pygame import *

class GUI:
	def __init__(self):
		pygame.init()

		self.screen = pygame.display.set_mode((800,600))
		
		self.bg = pygame.Surface((800, 300)).convert()
		self.bg.fill((175, 175, 200))
		self.fg = None
		
		self.consolebg = pygame.Surface((800, 300)).convert()
		self.consolebg.fill((0,0,0))
		
		font = pygame.font.Font(None, 16)
		self.font = font
		self.out = []
		self.start = 0
		self.vis = 16
		self.redraw()
		self.die = 0
		
	def addLine(self, line):
		self.out.append(line)
		self.down()
		self.redraw() #to ensure we redraw even if down() doesnt do it for us.
		
	def popLines(self, num):
		self.out = self.out[0 : len(self.out) - num]
		self.end()
		
	def up(self):
		if self.start > 0:
			self.start -= 1
			self.redraw()
			
	def down(self):
		if self.start < len(self.out) - self.vis :
			self.start += 1
			self.redraw()
		
	def home(self):
		self.start = 0
		self.redraw()
		
	def end(self):
		self.start = len(self.out) - self.vis
		if self.start < 0:
			self.start = 0
			
		self.redraw()
		
	def img(self, image):
		self.bg = pygame.image.load(image).convert_alpha()
		self.redraw()
		
	def img2(self, image):
		if image is not None:
			self.fg = pygame.image.load(image).convert_alpha()
		else:
			self.fg = None
			
		self.redraw()
		
	def bottom(self, image):
		self.consolebg = pygame.image.load(image).convert_alpha()
		self.redraw()

	def redraw(self):
		self.screen.blit(self.bg, (0, 0))
		if self.fg is not None:
			self.screen.blit(self.fg, (0, 0))
		
		self.screen.blit(self.consolebg, (0, 300))

		visible = self.out[self.start:self.start + self.vis]
		y = 315
		for item in visible:
			surf = self.font.render(item, 1, (255,255,255,255)).convert_alpha()
			self.screen.blit(surf, (25, y))
			y += 17
					